/**
 * Copyright (c) 2020 Coder League
 * All rights reserved.
 *
 * File：RoleAuthDaoImpl.java
 * History:
 *         2020年12月20日: Initially created, Chrise.
 */
package club.coderleague.cfss.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import club.coderleague.cfss.dao.extension.RoleAuthDaoExtension;
import club.coderleague.cfss.domain.beans.UserSession;
import club.coderleague.cfss.domain.entities.Roleauths;
import club.coderleague.cfss.domain.enums.EntityState;
import club.coderleague.cfss.util.HttpServletUtil;
import club.coderleague.data.jpa.id.support.SnowflakeV4;
import club.coderleague.data.jpa.repository.support.AbstractDataRepositoryExtension;

/**
 * 角色授权数据访问对象实现。
 * @author Chrise
 */
public class RoleAuthDaoImpl extends AbstractDataRepositoryExtension<Roleauths, Long, SnowflakeV4> implements RoleAuthDaoExtension {
	/**
	 * @see club.coderleague.cfss.dao.extension.RoleAuthDaoExtension#findFuncidByRoleid(java.lang.Long)
	 */
	@Override
	public List<String> findFuncidByRoleid(Long roleid) {
		String sql = "select concat(ra.funcid) from roleauths ra where ra.roleid = ?0 and ra.entitystate = ?1";
		return super.queryStringListBySql(sql, roleid, EntityState.VALID.getValue());
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.RoleAuthDaoExtension#updateEntitystateByRoleidNotInEntityids(java.lang.Integer, java.lang.Long, java.util.List)
	 */
	@Override
	public Integer updateEntitystateByRoleidNotInEntityids(Integer entitystate, Long roleid, List<Long> validentityids) {
		StringBuilder sqlBuilder = new StringBuilder("update roleauths set modifier = :modifier, modifytime = :modifytime, entitystate = :entitystate"
			+ " where roleid = :roleid and entitystate = :validstate");
		Map<String, Object> params = new HashMap<>();
		params.put("validstate", EntityState.VALID.getValue());
		// 获取用户信息
		UserSession us = HttpServletUtil.getUserSession();
		params.put("modifier", us.getUserid());
		params.put("modifytime", new Date());
		params.put("entitystate", entitystate);
		params.put("roleid", roleid);
		if (validentityids != null && !validentityids.isEmpty()) {
			sqlBuilder.append(" and entityid not in (:validentityids)");
			params.put("validentityids", validentityids);
		}
		return super.executeUpdateBySql(sqlBuilder.toString(), params);
	}
}
